package com.xiaoyu.lanling.event.media.mediaselector;

import com.xiaoyu.base.event.BaseEvent;
import com.xiaoyu.base.utils.ListUtil;
import com.xiaoyu.media.matisse.internal.entity.SelectionItem;
import com.xiaoyu.media.matisse.internal.entity.SelectionResult;
import com.xiaoyu.lanling.media.selector.MediaSelectorFromType;
import com.xiaoyu.lanling.media.selector.MediaSelectorType;

import java.util.ArrayList;
import java.util.List;

public class MediaSelectorResultEvent extends BaseEvent {

    @MediaSelectorType.MediaSelectorTypeDef
    public final String type;
    @MediaSelectorFromType.MediaSelectorFromTypeDef
    public final int fromType;
    public final SelectionResult result;
    public final boolean original;
    public final List<String> mediaPathList = new ArrayList<>();
    public final List<String> imagePathList = new ArrayList<>();
    public final List<String> gifPathList = new ArrayList<>();
    public final List<String> videoPathList = new ArrayList<>();

    public MediaSelectorResultEvent(@MediaSelectorType.MediaSelectorTypeDef final String type,
                                    @MediaSelectorFromType.MediaSelectorFromTypeDef int fromType,
                                    SelectionResult result, boolean original) {
        this.type = type;
        this.fromType = fromType;
        this.result = result;
        this.original = original;

        convertItemListToPathList();
    }

    public boolean isMediaEmpty() {
        return ListUtil.isNullOrEmpty(mediaPathList);
    }

    private void convertItemListToPathList() {
        for (SelectionItem item : result.getMediaList()) {
            String path = item.getPath();
            mediaPathList.add(path);

            if (item.isGif()) { // Gif must before image, because MimeType of image/gif also is image
                gifPathList.add(path);
            } else if (item.isImage()) {
                imagePathList.add(path);
            } else if (item.isVideo()) {
                videoPathList.add(path);
            }
        }
    }
}
